package com.jp.knowledge.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.HeadItemAdapter;
import com.jp.knowledge.adapter.OnItemListerner;
import com.jp.knowledge.callback.ItemDragHelperCallback;
import com.jp.knowledge.db.DBHelper;
import com.jp.knowledge.model.HeadLine;

import java.util.List;

import rx.Subscriber;

/**
 * Created by jiayen on 16/10/10.
 */
public class ComPopuWindow extends PopupWindow implements View.OnClickListener {
    private View conentView;

    private Context mContext;
    //拖动gridView
    private RecyclerView sortView;
    private HeadItemAdapter headItemAdapter;
    private TextView sureBtn;//确定按钮
    private HeadItemAdapter adapter;

    private DragItemHelper  dragItemHelper;
    private List<HeadLine> list;

    private OnItemListerner onItemListerner;

    public void setOnItemListerner(OnItemListerner onItemListerner) {
        this.onItemListerner = onItemListerner;
    }

    public void setDragItemHelper(DragItemHelper dragItemHelper) {
        this.dragItemHelper = dragItemHelper;
    }

    public ComPopuWindow(final Activity context) {

        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.toutiao_fragment_sort, null);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
//        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
         this.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimationPreview);

        init();
    }

    private void init() {
        sureBtn = (TextView) conentView.findViewById(R.id.sure);
        sortView = (RecyclerView) conentView.findViewById(R.id.activity_sort_head);
        sureBtn.setOnClickListener(this);
        DBHelper.getInstance().findAll(HeadLine.class, new Subscriber<List<HeadLine>>() {
            @Override
            public void onCompleted() {
//                ToasUtil.toast(context, "onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                ToasUtil.toast(mContext, "获取数据失败");
            }

            @Override
            public void onNext(List<HeadLine> headLines) {
                list = headLines;
                if (headLines != null && headLines.size() > 0) {
                    bindViewData();
                }
            }
        });

    }


    /**
     * 绑定数据
     */
   private void bindViewData() {
       adapter = new HeadItemAdapter(mContext,list);
       adapter.setItemListerner(onItemListerner);
       adapter.setDragItemHelper(dragItemHelper);
       GridLayoutManager layoutMannager = new GridLayoutManager(mContext, 4, LinearLayoutManager.VERTICAL, false);

       sortView.setLayoutManager(layoutMannager);
       sortView.setItemAnimator(new DefaultItemAnimator());
       sortView.setAdapter(adapter);
       initItemDrag();
    }


    public List<HeadLine> getHeadLine(){
        return adapter.getData();
    }
    /**
     * 初始化拖拽
     */
    private void initItemDrag() {
        ItemDragHelperCallback itemDragHelperCallback = new ItemDragHelperCallback(adapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragHelperCallback);
        itemTouchHelper.attachToRecyclerView(sortView);
        adapter.setItemTouchHelper(itemTouchHelper);
        adapter.setItemDragHelperCallback(itemDragHelperCallback);
    }

    public void updateData(){//刷新数据
        adapter.notifyDataSetChanged();
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent, List<HeadLine> headLines) {
        if(list == null || list.size() == 0) {
            list = headLines;
            bindViewData();
        }
        if (!this.isShowing()) {
            conentView.setBackgroundResource(R.color.white);
            // 以下拉方式显示popupwindow
            showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0);
        } else {
            this.dismiss();
        }
    }

    public List<HeadLine> getList() {
        return list;
    }

    @Override
    public void onClick(View view) {
        this.dismiss();
    }

    public interface  DragItemHelper{
         void drag(int fromPositon,int toPosition);
        void dragFinish();
    }
}
